#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <numeric> // 包含fill accumulate
using namespace std;
template <class T>
class MyPrint
{
public:
    void operator()(T val)
    {
        cout << val << " ";
    }
};
void test1()
{
    vector<int> v1;
    for (int i = 1; i < 10; ++i)
    {
        v1.push_back(i);
    }
    srand((unsigned int)time(NULL));
    // 虽然报错未找到未定义标识符，但是依然可以编译
    random_shuffle(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), MyPrint<int>());
}
void test2()
{
    vector<int> v1, v2, v3;
    for (int i = 1; i < 6; ++i)
    {
        v1.push_back(i);
        v1.push_back(i+2);
    }
    v3.resize(v1.size() + v2.size());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    for_each(v3.begin(), v3.end(), MyPrint<int>());
    cout << endl;
    // reverse
    reverse(v3.begin(), v3.end());
    for_each(v3.begin(), v3.end(), MyPrint<int>());
    cout << endl;
}
template<class T>
class MyGreater
{
public:
    int m_base = 0;
    MyGreater(int base): m_base(base) {}
    bool operator()(int val)
    {
        return val > m_base;
    }
};
void test3()
{
    vector<int> v1, v2;
    for (int i = 1; i < 6; ++i)
    {
        v1.push_back(i);
    }
    v2.resize(v1.size());
    // copy
    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), MyPrint<int>());
    cout << endl;
    // replace
    replace(v2.begin(), v2.end(), 3, -1);
    for_each(v2.begin(), v2.end(), MyPrint<int>());
    cout << endl;
    // replace_if
    replace_if(v2.begin(), v2.end(), MyGreater<int>(3), 9);
    for_each(v2.begin(), v2.end(), MyPrint<int>());
    cout << endl;
}
void test4()
{
    vector<int> v1, v2;
    for (int i = 0; i < 101; ++i)
    {
        v1.push_back(i);
    }
    // numeric头文件中accumulate
    int res = accumulate(v1.begin(), v1.end(), 10);
    cout << res << endl;
    v2.resize(9);
    fill(v2.begin(), v2.end(), 10);
    for_each(v2.begin(), v2.end(), MyPrint<int>());
    cout << endl;
}
// 求两个容器 交集intersection 并集union 差集difference
void test5()
{
    vector<int> v1, v2, v3, v4, v5, v6;
    for (int i = 1; i < 10; ++i)
    {
        v1.push_back(i);
        v2.push_back(2*i);
    }
    for_each(v1.begin(), v1.end(), MyPrint<int>());
    cout << endl;
    for_each(v2.begin(), v2.end(), MyPrint<int>());
    cout << endl;

    // 交集 返回最后一个元素迭代器
    cout << "set_intersection: " << endl;
    v3.resize(min(v1.size(), v2.size()));
    vector<int>::iterator itEnd1 = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    for_each(v3.begin(), itEnd1, MyPrint<int>());
    cout << endl;
    // 并集 
    cout << "set_union: " << endl;
    v4.resize(v1.size() + v2.size());
    vector<int>::iterator itEnd2 = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin());
    for_each(v4.begin(), itEnd2, MyPrint<int>());
    cout << endl;
    // 差集
    cout << "set_difference: " << endl;
    cout << "v1 - v2: ";
    v5.resize(max(v1.size(), v2.size()));
    vector<int>::iterator itEnd3 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v5.begin());
    for_each(v5.begin(), itEnd3, MyPrint<int>());
    cout << endl;
    cout << "v2 - v1: ";
    v6.resize(max(v1.size(), v2.size()));
    vector<int>::iterator itEnd4 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v6.begin());
    for_each(v6.begin(), itEnd4, MyPrint<int>());
    cout << endl;

}
int main(void)
{
    // test1();
    // test2();
    // test3();
    // test4();
    test5();

    return 0;
}